    
    
var MyBuyPrice = 0; // global to hold the price the user sets
var MyBuyProd = ''; // the product the user wants to buy
var bAletSent = false;

// called from display, sets MyBuyPrice
function SwakProductSearchBuyAlert() {	
    var p = 0;
    
    var alertInput = sBuyAlert();
    if (alertInput==null || alertInput=='') 
        return;
    p = alertInput.replace('$','');
    if ( IsNumeric(p)) 
    {
        if (p==0) // reset our buy price!
        {
         resetBuyPrice();
         return;   
        }
        MyBuyPrice = p;
    } 
    else 
    {
        alert("Price must be a dollar amount. Buy price not set.");						
    }

}

function getProduct() 
{
    var retVal;
    var product = swaksmack.SmackShopDoc.getElementById('productNameAnchor');
    
    if (product==null)
    {
        return null;
    }
    
    return product.textContent;
    
}

// prompt for set price    
function sBuyAlert() 
{

	var state = SMACK_STATE_UNKNOWN;
	if (swaksmack.smackShopContentWindow != null) {
		state = swaksmack.SS_JF.getState();
	}
	
    var keyword = getProduct();

    if (keyword == null || state == SMACK_STATE_GURUGUESSSOLD || state == SMACK_STATE_UNKNOWN) 
        return; // bailout, we can't find the product or it just sold on us, or i just dunno
    
    MyBuyProd = keyword; // set our global cache
   
    var deflt = '$10.00';
    
    if (MyBuyPrice != 0 ) 
    {
        deflt = '$' + MyBuyPrice;
    }

    return prompt("Please enter your buy price for "+keyword + "\r\nZero (0) clears your buy alert.",deflt);
}

// pops the buy alerts when the price hits 
function popBuyAlert() 
{

    var smackpollAlertPref = SS_getPreference(SMACK_SMACKPOTALERT);
    
    if (bAletSent)
        return;
        
        bAletSent = true; // so we do not keep popping messages!
        


    if (smackpollAlertPref == "both" || smackpollAlertPref == "audio") 
    {
        var soundFile = SS_getPreference(SMACK_AUDIOSOUND_OUTWIT); // plays the outwit sound.. should probable change this at some point
        if (soundFile == null || soundFile == '') 
        {
            soundFile = AUDIO_DIR + DEFAULT_SOUND;
        }
        swaksmack.audioManager.playAudioAlert(soundFile);
    }
    if (smackpollAlertPref == "both" || smackpollAlertPref == "alert")
    {
    //alert('smack pot alert');
    var leftPanel = swaksmack.SmackShopDoc.getElementById('leftPanel');
    if (leftPanel != null) {
    leftPanel.addEventListener('click',  function(event) {
    swaksmack.alertManager.alertWindow.close();
    //remove this event listener
    this.removeEventListener('click',arguments.callee,false);
    }, false);
    }
    swaksmack.alertManager.swakAlert(SMACK_ALERT_BUYALERT, true, 60000, true);
    }

}

function resetBuyPrice() 
{
            MyBuyPrice = 0; 
            MyBuyProd = '';
            bAletSent = false;
}

// evaluates price and buy price to see if we need to alert
function processMyBuyPrice(handle) 
{
	if (bAletSent == true) return;
	
    var curPrice = 0;
    
    var item = swaksmack.SmackShopDoc.getElementById('productNameAnchor');
    var retail = swaksmack.SmackShopDoc.getElementById('retailPrice');
    var cost = swaksmack.SmackShopDoc.getElementById('totalCost');
    
    if (item==null || retail == null || cost == null)         
        return;
        
    
    saleTT = item.textContent + '   Retail: ' + retail.textContent;		    
    salelabel = cost.textContent;


    
    // a little sanity check
    
    if (salelabel==null || handle == null || saleTT == null || getProduct() != MyBuyProd)   
    {
        // reset, the deal may have ended                    
            resetBuyPrice();
            return; // bail
    }      
            
    
    // if we have set a buy price, evaluate it and alert if need be.
    if (Number(MyBuyPrice) > 0) 
    {
    
        curPrice = salelabel.replace('$', '');			    			    

        if (IsNumeric(curPrice) && Number(MyBuyPrice) < Number(curPrice)) 
        {        
            handle.saleStatusPanel.label= salelabel + ' / $' + MyBuyPrice  ;	 // we are still waiting for it to hit our buy price.	
            handle.saleStatusPanel.tooltipText=saleTT + ' Alert: $' + MyBuyPrice;
        } 
        else 
        {
            // handle.saleStatusPanel.label="buy now"; // Alert the user to buy now!
            
            if (SS_getPreference(SMACK_ALERTGLOABLEENABLE)) 
            {
                popBuyAlert();          
            }
        }			   
    }
    else	
    {		
        handle.saleStatusPanel.label=salelabel;
        handle.saleStatusPanel.tooltipText=saleTT;
    }
}